package com.ce.framework.datamaintain.logic;

import java.util.List;

import com.ce.framework.exception.AppException;
import com.ce.framework.util.StringUtil;
import com.ce.sys.dict.dao.po.SysDictItem;
import com.ce.util.DictUtil;

public class AdvSearchCond {

	/** 查询条件文本 */
	private String text;
	/** 查询用的字段 */
	private String field;
	/** 类型text文本date日期num数字sel选择 */
	private String type;
	/** type=text时需要此字段，默认值为LIKE，可以选用其它比较符号 */
	private String sign;
	/** 对自动生成的input或select进行js脚本处理，此字段应存储JS方法，如生成下拉树，下拉框等 格式如function(){...} */
	private String call;
	/** type=date时做为日期框的格式串 */
	private String format;
	/** type=sel时做为选择框的内容填充 */
	private String data,dictName;

	public AdvSearchCond(String text, String field, String type) {
		super();
		this.text = text;
		this.field = field;
		this.type = type;
	}

	public AdvSearchCond(String text, String field, String type, String sign) {
		super();
		this.text = text;
		this.field = field;
		this.type = type;
		this.sign = sign;
	}

	public AdvSearchCond(String text, String field, String type, String sign, String call) {
		super();
		this.text = text;
		this.field = field;
		this.type = type;
		this.sign = sign;
		this.call = call;
	}

	public AdvSearchCond(String text, String field, String type, String sign, String call, String data, String format) {
		super();
		this.text = text;
		this.field = field;
		this.type = type;
		this.sign = sign;
		this.call = call;
		this.format = format;
		this.data = data;
	}
	
	public static AdvSearchCond genSel(String text,String field,String dictName){
		AdvSearchCond c = new AdvSearchCond(text,field,"sel");
		c.setSelDictName(dictName);
		return c;
	}

	public static final String TYPE_TEXT = "text";
	public static final String TYPE_DATE = "date";
	public static final String TYPE_NUM  = "num";
	public static final String TYPE_SEL  = "sel";

	/** type=sel时将公共字典项名称通过此方法传入，可自动产生data属性 */
	public void setSelDictName(String name){
		dictName = name;
	}
	
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getField() {
		return field;
	}
	public void setField(String field) {
		this.field = field;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getSign() {
		return sign;
	}
	public void setSign(String sign) {
		this.sign = sign;
	}
	public String getCall() {
		return call;
	}
	public void setCall(String call) {
		this.call = call;
	}
	public String getFormat() {
		return format;
	}
	public void setFormat(String format) {
		this.format = format;
	}
	public String getData() {
		if(StringUtil.isBlank(data)&&StringUtil.isNotBlank(dictName)){
			StringBuffer html = new StringBuffer();
			try {
				List<SysDictItem> dictItemList = DictUtil.getDictListByName(dictName);
				for(SysDictItem opt:dictItemList){
					html.append(",").append(getHtmlVal(opt.getVcItemNumber())).append(":").append(getHtmlVal(opt.getVcItemName()));
				}
			} catch (AppException e) {
				e.printStackTrace();
			}
			data = html.length()>0?html.substring(1):"";
		}
		return data;
	}
	public void setData(String data) {
		this.data = data;
	}

	private static String getHtmlVal(String val){
		return val==null?"":val.replaceAll(",", "&#44;").replaceAll(":", "&#58;").replaceAll("'", "&#39;").replaceAll("\\r|\\n", "");
	}
}
